// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors;

import org.dengues.commons.IDenguesCoreContants;
import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.designer.ui.DesignerUIPlugin;
import org.dengues.designer.ui.database.editors.DBFileEditorInput;
import org.dengues.designer.ui.database.editors.DenguesSqlEditor;
import org.dengues.designer.ui.database.editors.GEFDatabaseEditor;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.query.ColumnsAliasFormPage;
import org.dengues.designer.ui.query.FiltersFormPage;
import org.dengues.designer.ui.query.GroupSortFormPage;
import org.dengues.designer.ui.query.HavingFormPage;
import org.dengues.designer.ui.query.UnionFormPage;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.EMFDataType;
import org.dengues.ui.editors.AbstractGenericGEFEditor;
import org.dengues.ui.editors.AbstractGenericMultiPageEditor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-1-15 qiang.zhang $
 * 
 */
public class DatabaseMultiEditor extends AbstractGenericMultiPageEditor {

    public static final String ID = "org.dengues.designer.ui.editors.databaseMultiEditor"; //$NON-NLS-1$

    private final GEFDatabaseEditor gefDBEditor = new GEFDatabaseEditor();

    private final DenguesSqlEditor sqlEditor = new DenguesSqlEditor();

    private boolean isQuery;

    /**
     * Qiang.Zhang.Adolf@gmail.com DatabaseMultiEditor constructor comment.
     */
    public DatabaseMultiEditor() {
        setTitleImage(ImageUtil.getImage(IDenguesSharedImage.VIEW_DATA));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPage1".
     */
    private void createPageOthers() {
        try {
            DBFileEditorInput dbEditorInput = (DBFileEditorInput) getEditorInput();
            DatabaseDiagram dbDiagram = dbEditorInput.getDbDiagram();
            EMFDataType emfType = dbDiagram.getEmfType();
            isQuery = EMFDataType.QUERY == emfType.getValue();
            if (isQuery) {
                addPage(new ColumnsAliasFormPage(this));
                addPage(new FiltersFormPage(this));
                addPage(new GroupSortFormPage(this));
                addPage(new HavingFormPage(this));
                addPage(new UnionFormPage(this));
                getSite().getPage().showView(IDenguesCoreContants.QUERY_VIEW_ID);
            }
            // else if (EMFDataType.DATABASE == emfType.getValue()) {
            int index = addPage(sqlEditor, dbEditorInput);
            setPageText(index, Messages.getString("DatabaseMultiEditor.SqlEdit")); //$NON-NLS-1$
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPage0".
     */
    private void createPage0() {
        try {
            int index = addPage(gefDBEditor, getEditorInput());
            setPageText(index, Messages.getString("DatabaseMultiEditor.Designer.Label")); //$NON-NLS-1$
            this.setPartName(gefDBEditor.getPartName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.forms.editor.FormEditor#createToolkit(org.eclipse.swt.widgets.Display)
     */
    @Override
    protected FormToolkit createToolkit(Display display) {
        return new FormToolkit(DesignerUIPlugin.getDefault().getFormColors(display));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.MultiPageEditorPart#pageChange(int)
     */
    @Override
    protected void pageChange(int newPageIndex) {
        Object page = pages.get(newPageIndex);
        if (page == sqlEditor) {
            try {
                sqlEditor.doSetInput(getEditorInput());
            } catch (CoreException e) {
                e.printStackTrace();
            }
        } else if (isQuery) {
            // Now is the absolute last moment to create the page control.
            if (page instanceof IFormPage) {
                IFormPage fpage = (IFormPage) page;
                if (fpage.getPartControl() == null) {
                    fpage.createPartControl(getContainer());
                    setControl(newPageIndex, fpage.getPartControl());
                    fpage.getPartControl().setMenu(getContainer().getMenu());
                }
            }
        }
    }

    private boolean propertyIsDirty = false;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
     */
    @Override
    public void doSave(IProgressMonitor monitor) {
        if (!isDirty()) {
            return;
        }
        for (int i = 0; i < getPageCount(); i++) {
            Object page = pages.get(i);
            if (page instanceof IFormPage) {
                IFormPage fpage = (IFormPage) page;
                fpage.doSave(monitor);
            }
        }
        getEditor(0).doSave(monitor);
        propertyIsDirty = false;
        firePropertyChange(IEditorPart.PROP_DIRTY);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getGefDBEditor".
     * 
     * @return
     */
    public GEFDatabaseEditor getGefDBEditor() {
        return this.gefDBEditor;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
     */
    @Override
    public boolean isDirty() {
        return propertyIsDirty || super.isDirty();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
     */
    @Override
    protected void addPages() {
        createPage0();
        createPageOthers();
        setActivePage(GEFDatabaseEditor.ID);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.editor.AbstractGenericMultiPageEditor#getDesignerEditor()
     */
    @Override
    public AbstractGenericGEFEditor getDesignerEditor() {
        return gefDBEditor;
    }

}
